import VueRouter from "vue-router";
import MyLogin from "../components/MyLogin";
import MyRegister from "../components/MyRegister";
import MyHome from "../components/MyHome";
import MyChat from "../components/home/MyChat";
import AddUser from "../components/home/AddUser";
import MyInfo from "../components/home/MyInfo"


const router=new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "login",
    },
    {
      path: "/login",
      name: "login",
      component: MyLogin,
    },
    {
      path: "/register",
      component: MyRegister,
    },
    {
      path: "/home",
      component: MyHome,
      children: [
        {
          path: "chat",
          component: MyChat,
        },
        {
          path: "add",
          component: AddUser,
        },
        {
          path:"info",
          component:MyInfo,
        }
      ],
    },
  ],
});

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router